package com.neusoft.neuiotms.dcom.service.incident.impl;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.bizcore.activiti.dto.WorkorderDTO;
import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.activiti.service.WorkOrderService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.converter.incident.IncidentCloseConverter;
import com.neusoft.neuiotms.dcom.dto.KnowledgeDTO;
import com.neusoft.neuiotms.dcom.dto.incident.IncidentCloseDTO;
import com.neusoft.neuiotms.dcom.dto.incident.IncidentHandleDTO;
import com.neusoft.neuiotms.dcom.model.incident.IncidentClose;
import com.neusoft.neuiotms.dcom.repository.incident.IncidentCloseRepository;
import com.neusoft.neuiotms.dcom.service.KnowledgeService;
import com.neusoft.neuiotms.dcom.service.incident.IncidentCloseService;
import com.neusoft.neuiotms.dcom.service.incident.IncidentHandleService;

/**
 * @author WYS
 * @date 2020年8月18日 下午2:06:12
 */
@Service
public class IncidentCloseServiceImpl implements IncidentCloseService {
    @Autowired
    private IncidentCloseRepository incidentCloseRepository;
    @Autowired
    private IncidentCloseConverter incidentCloseConverter;
    @Autowired
    private ProcessInstanceService processInstanceService;
    @Autowired
    private KnowledgeService knowledgeService;
    @Autowired
    private IncidentHandleService incidentHandleService;
    @Autowired
    private WorkOrderService workOrderService;

    @Override
    @Transactional
    public IncidentCloseDTO create(final IncidentCloseDTO dto, final UserBean user) {
        // 1. 获取处理信息
        final IncidentHandleDTO handleDTO = this.incidentHandleService.findByWorkNum(dto.getWorkOrderNum());
        if (handleDTO != null) {
            // 存入知识库
            final KnowledgeDTO knowledgeDTO = new KnowledgeDTO();
            final WorkorderDTO workorderDTO = this.workOrderService.details(dto.getWorkOrderNum());
            knowledgeDTO.setTitle(workorderDTO.getTitle());
            knowledgeDTO.setContent(
                    " <p> 事件原因 -> " + handleDTO.getEventCause() + " </p> " +
                            " <p> 处理方案 -> " + handleDTO.getPlan() + " </p> ");
            knowledgeDTO.setKnowledgeTypeCode("incident");
            knowledgeDTO.setKnowledgeTypeCode("GZCLGF");
            this.knowledgeService.create(knowledgeDTO, user);
        }

        // 2. save
        // 查询当前workNum和TaskId下是否存在记录
        final IncidentClose oldModel = this.incidentCloseRepository.findByWorkNumAndTaskId(
                dto.getWorkOrderNum(), dto.getTaskId());
        if (oldModel == null) { // 如果不存在新增
            dto.setId(null);
        }

        final IncidentClose model = this.incidentCloseConverter.toModel(dto);
        this.incidentCloseRepository.save(model);

        // 3. 完成节点
        this.processInstanceService.complete(dto.getWorkOrderNum(), dto.getTaskId(), user);

        return this.incidentCloseConverter.toDTO(model);
    }

    @Override
    public IncidentCloseDTO detail(final String workOrderNum, final String taskId, final UserBean user) {
        IncidentClose model = null;
        try {
            if (StringUtils.isNotBlank(workOrderNum) && !"NULL".equals(taskId)) {
                model = this.incidentCloseRepository.findByWorkNumAndTaskId(workOrderNum, taskId);
                if (model == null) {
                    // 获取前一节点的TaskID
                    final String fromProcessTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (fromProcessTaskId == null) {
                        return new IncidentCloseDTO();
                    } else {
                        model = this.incidentCloseRepository.findByWorkNumAndTaskId(workOrderNum,
                                fromProcessTaskId);
                    }
                }
            } else if ("NULL".equals(taskId)) {
                model = this.incidentCloseRepository.findByWorkNum(workOrderNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workOrderNum },
                    new Object[] {taskId });
        }
        return this.incidentCloseConverter.toDTO(model);
    }

    @Override
    public IncidentCloseDTO findByWorkNumAndTaskId(final String workNum, final String taskId) {
        final IncidentClose model = this.incidentCloseRepository.findByWorkNumAndTaskId(workNum, taskId);
        return this.incidentCloseConverter.toDTO(model);
    }

    @Override
    public void deleteByWorkNum(final String workNum) {
        this.incidentCloseRepository.deleteByWorkNum(workNum);
    }
}
